//
//  mineIndexViewController.swift
//  NewLndd
//
//  Created by 邵禹曦 on 2020/1/7.
//  Copyright © 2020 辽宁当代. All rights reserved.
//

import UIKit
// MARK: - 我的首页
class mineIndexViewController: ApiViewController {
    @IBOutlet weak var nicheng: UILabel!
    @IBOutlet weak var _user: UILabel!
    
    @IBOutlet weak var U: UILabel!
    @IBOutlet weak var cj: UIButton!
    @IBOutlet weak var cx: UIButton!
    @IBOutlet weak var ch: UIButton!
    @IBOutlet weak var sz: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        showInfo()
    }
    



}


extension mineIndexViewController{
    private func showInfo() {
           // GCD
           let group = DispatchGroup()
    
           // 从沙盒中获取参数
            var userPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
            userPath =  (userPath as NSString).appendingPathComponent("data.plist")
            user = NSKeyedUnarchiver.unarchiveObject(withFile: userPath) as? User

Thread.sleep(forTimeInterval: 0.05)
           // 用户验证
           self.u = user?.U!
           self.retcode = user?.RETCODE!
           // 用户验证
           let str = [u,"101",retcode]
            
           //self.isLogin = self._network(str: str as! [String], group: group)
        group.enter()
        NetworkTools.share.check_user(code: str as Array<Any>) { (result, error) in
                      
                          if error != nil {
                              print("no no no")
                              return
                          }
                          

                          if Int(self._retcode(res: result!))! < 0 {
                              self.isLogin = false
                          } else {
                              self.isLogin = true
                          }
                          group.leave()
                      }

           group.notify(queue: DispatchQueue.main) {
               if !self.isLogin {
                   return self.setupVisitorView()
               } else {
                Thread.sleep(forTimeInterval: 0.05)
                   // 2.设置导航栏内容
                   self.setupNavigationBar()
                   //self.showTabBtn()
                self.tableShouInfo()
               }
            }
       }
}


extension mineIndexViewController{
    private func setupNavigationBar() {
        // 监听按钮
        cj.addTarget(self, action: #selector(topBtn), for: .touchUpInside)
        cx.addTarget(self, action: #selector(topBtn1), for: .touchUpInside)
        ch.addTarget(self, action: #selector(topBtn2), for: .touchUpInside)
        sz.addTarget(self, action: #selector(topBtn3), for: .touchUpInside)
    }
    
    @objc private func topBtn() {
        self.performSegue(withIdentifier: "cj", sender: nil)
    }
    
    @objc private func topBtn1() {
        self.performSegue(withIdentifier: "cx", sender: nil)
    }
    
    @objc private func topBtn2() {
        self.performSegue(withIdentifier: "ch", sender: nil)

    }
    @objc private func topBtn3() {
        self.performSegue(withIdentifier: "sz", sender: nil)

    }
    
    // MARK: - 跳转
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // 设置
        if segue.identifier == "sz" {
            let secVC = segue.destination as! settingsViewController
            secVC._comInfo = sender as? String
        }
        
        // 出金
        if segue.identifier == "cj" {
            let secVC = segue.destination as! goldViewController
            secVC._comInfo = sender as? String
        }
        
        // 存货
        if segue.identifier == "ch" {
            let secVC = segue.destination as! stockViewController
            secVC._comInfo = sender as? String
            
        }
        
        // 查询
        if segue.identifier == "cx" {
            let secVC = segue.destination as! inquiryViewController
            secVC._comInfo = sender as? String
        }
    }
}

//userinfo_query
extension mineIndexViewController{
    // MARK: - TableViewShowInfo
    @objc private func tableShouInfo() {
        
        let str = [user?.U!,user?.RETCODE! ]
        
        NetworkTools.share.userinfo_query(code: str as Array<Any>) { (result, error) in
            
            if error != nil {
                print("no no no")
                return
            }
            
            // 获取可选类型中的数组
            guard let resultArray = result else {
                print("no")
                return
            }
            if let REP = resultArray["REP"] as? [String : AnyObject] {
                if let arr = REP["RESULT"] as? [String : AnyObject]   {
                    self.U.text = arr["U"] as? String
                    self._user.text = arr["UUN"] as? String
                    self.nicheng.text = arr["RN"] as? String
                    let code : Int = Int(arr["RETCODE"] as! String)!
                    if(Int(truncating: NSNumber(value: code)) < 0) {
                        if #available(iOS 13.0, *) {
                            self.showMsgbox(_message: "数据加载失败")
                            return self.setupVisitorView()
                        }
                    }
                }
            }
            
        }
        
        
    }
}
